# 独立使用django的model
import os
import sys

# 获取当前文件的路径
# os.path.realpath(__file__) 获取当前执行脚本的绝对路径。
# os.path.dirname() 获取当前脚本所在的文件夹名称


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 找到项目文件
sys.path.extend([BASE_DIR, ])

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "XUEKE.settings")

import django

django.setup()
from practice.models import Employees, Departments, User
from django.db import reset_queries, connection
from model_bakery import baker

# zhang_san = User.objects.create(username='zhangsan', first_name='tom',
#                                 last_name='Sam', email='238551@qq.com')

u = User.objects.get(username='zhangsan')

qs = Expense.objects.filter().values('pay_date').annotate(total=Sum('amount'),
                                                          pay_date_str=Cast('pay_date', CharField())  # 将时间转换成字符串
                                                          ).values_list('pay_date_str', 'total').order_by('total')
print(qs)
result = [(i, 0) for i in range(1, 13)]
qs = Expense.objects.filter(pay_type=PayType.SPENDING).annotate(month=ExtractMonth('pay_date')).values(
    'month').annotate(
    total=Sum('amount')).values_list('month', 'total').order_by()
print(result)
print(qs)
result.update(qs)
print(result)
